package com.lvmama.rhino.analyze.client

import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper}
import com.lvmama.rhino.analyze.processing._
import com.lvmama.rhino.common.entity.{JobConfig, PerformaceMonitorLog}
import com.lvmama.rhino.common.utils.logging.LazyLogging
import com.lvmama.rhino.common.utils.{GZipUtils, Utils}
import com.lvmama.rhino.common.utils.spark.{SparkApplication, SparkStreamingApplication}
import org.apache.commons.codec.binary.Base64
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.functions._
import org.apache.spark.storage.StorageLevel

import collection.JavaConversions._
import org.slf4j.{Logger, LoggerFactory}


/**
  * author:钱清晨
  *
  * 客户端接口性能监控
  * 监测接口的成功与失败以及监测不同地区接口调用网络状况，根据对应的数据来优化接口以及对应的客户端版本和CDN
  *
  * 对应的WIKI为
  * http://192.168.0.63:81/wiki/index.php?title=%E7%9B%91%E6%8E%A7%E7%B3%BB%E7%BB%9F1.0.0%E6%8E%A5%E5%8F%A3
  * 对应的表为
  * monitor_api
  * monitor_api_failure
  * monitor_app
  * monitor_region
  * monitor_scenic_spot  这个表为分析不同的景区当前的人数以及网络情况
  *
  */
class PerformanceMonitor(config: JobConfig, path: String) extends SparkApplication  with LazyLogging {

  override var appName: String = "PerformanceMonitor"
  override var sparkConfig: Map[String, String] = config.spark

  def execute(): Unit = {
    sparkConfig += ("spark.app.name" -> appName)
    sparkConfig += ("spark.master" -> "local[3]")
    withSparkContext { sc =>
      val sqlCtx = new SQLContext(sc)
      import sqlCtx.implicits._
      //预处理
      val yesterday = Utils.getYesterdayTimeInMillis()
      val preLog = sc.textFile(path).map(l => PerformanceMonitor.prePerformanceMonitor(l))
      val log =  preLog.flatMap{
        x =>x._15.map(y =>PerformaceMonitorLog.process(x._1, x._2, x._3, x._4, x._5, x._6, x._7, x._8, x._9, x._10, x._11, x._12, x._13, x._14, y, yesterday))
      }.toDF()
      val fullLog =  preLog.flatMap{
        x =>x._15.map(y =>PerformaceMonitorLog.fullProcess(x._1, x._2, x._3, x._4, x._5, x._6, x._7, x._8, x._9, x._10, x._11, x._12, x._13, x._14, y, yesterday))
      }.toDF()
      val columns = Seq("operators","networkType","deviceToken","tec","version","province","city","mobileNumber","userId","networkSpeed","osVersion",
        "browser","deviceName","interfaceUniqueFlag","pageId","networkTime","apiTime","apiName","apiVersion","requestType","time","errorCode")
      val result = log.filter(!col("apiName").equalTo("")).dropDuplicates(columns).persist(StorageLevel.MEMORY_AND_DISK_SER)
      val fullResult = fullLog.filter(!col("apiName").equalTo("")).dropDuplicates(columns)

     /* //接口数据
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorApi")
      PerformanceMonitorApi.process(sc, result)
      //接口失败数据
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorFailure")
      PerformanceMonitorFailure.process(sc, result)
      //接口地域数据
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorRegion")
      PerformanceMonitorRegion.process(sc, result)*/

      //地区接口信息
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorV2Region")
      PerformanceMonitorV2Region.process(sc, result)

      //用户网速
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorV2ApiNS")
      PerformanceMonitorV2ApiNS.process(sc, result)

      //接口数据
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorV2Api")
      PerformanceMonitorV2Api.process(sc, result)

      //接口错误数据
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorV2Failure")
      PerformanceMonitorV2Failure.process(sc, result)

      //接口历史数据
      logger.info(s"PerformanceMonitor begin process PerformanceMonitorV2History")
      PerformanceMonitorV2History.process(sc, fullResult)

      result.unpersist()
    }
  }
}

object PerformanceMonitor  extends LazyLogging  {
  def apply(path: String): Unit = {
    val config = JobConfig()
    new PerformanceMonitor(config, path).execute()
  }

  private val provArr:Array[String] = Array("安徽" ,"上海" ,"河北" ,"广东" ,"北京" ,"山东" ,"吉林" ,"辽宁" ,"四川" ,
    "黑龙江" ,"贵州" ,"河南" ,"陕西" ,"湖南" ,"天津" ,"浙江" ,"重庆" ,"江苏" ,"云南" ,"山西" ,"湖北" ,"甘肃" ,"福建"
    ,"海南" ,"江西" ,"宁夏" ,"广西" ,"新疆","西藏" ,"内蒙古" ,"香港" ,"澳门" ,"青海" ,"台湾")

  /*
   省份处理，将自治区和香港澳门进行转换，另外剔除掉国外的数据
  */
  def dealProv(prov: String): String = {
    if(prov.contains("宁夏")) {
      "宁夏"
    }
    else if(prov.contains("广西")) {
      "广西"
    }
    else if(prov.contains("新疆")) {
      "新疆"
    }
    else if(prov.contains("西藏")) {
      "西藏"
    }
    else if(prov.contains("内蒙古")) {
      "内蒙古"
    }
    else if(prov.contains("香港")) {
      "香港"
    }
    else if(prov.contains("澳门")) {
      "澳门"
    }
    else if(prov.contains("上海")) {
      "上海"
    }
    else if(prov.contains("北京")) {
      "北京"
    }
    else if(prov.contains("天津")) {
      "天津"
    }
    else if(prov.contains("重庆")) {
      "重庆"
    }
    else if (!provArr.contains(prov)) {
      ""
    }
    else {
      prov
    }
  }

  /**
    * 将data参数进行json转换，得到自己想要的数据
    *
    * @param log
    * @return
    */
  def jsonPrasePerformanceMonitor(log: String,osVersion: String,deviceName: String,browser: String,ip: String,ipToProvince: String,ipToCity: String,firstChannel: String): (String, String, String, String, String, String, String, String, String, String, String, String, String, Double, Array[JsonNode]) = {
    val mapper = new ObjectMapper()
    val jsonNode =  mapper.readTree(log)
    //运营商
    val ot = jsonNode.path("ot").asText("")
    //网络类型
    val nt = jsonNode.path("nt").asText("")
    //设备号
    val dt = jsonNode.path("dt").asText("")
    //技术类型
    val tec = jsonNode.path("tec").asText("")
    //客户端版本号
    val ver = jsonNode.path("ver").asText("")
    //    //经度
    //    val lo = jsonNode.path("lo").toString
    //    //纬度
    //    val la = jsonNode.path("la").toString
    //省份
    var prov = jsonNode.path("prov").asText("")
    //城市
    var ct = jsonNode.path("ct").asText("")
    //手机号
    val mn = jsonNode.path("mn").asText("")
    //账号
    var ui = jsonNode.path("ui").asText("")
    //公网速度
    val ns = jsonNode.path("ns").asDouble()*8

    val emptyArray = new Array[JsonNode](0)
    //参数校验  符合double格式0.00 || mn.equals("") || ui.equals("")
    if (prov.equals("") || ot.equals("") || nt.equals("") || dt.equals("") || tec.equals("") || ver.equals("") ) {
      return ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
    }
//    if (ns == 0.0) {
//      return ("", "", "", "", "", "", "", "", "" , "", "", "", "",0.0 , emptyArray)
//    }
    if(prov.equals("NONE") || ct.equals("NONE")){
      prov = ipToProvince.replaceAll("省","").replaceAll("市","")
      ct = ipToCity
    }else{
      prov = dealProv(prov)
    }

    if ("".equals(prov)) {
      return ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
    }
    //接口请求信息jsonarray
    val data = jsonNode.path("data")
    if (data.size() == 0) {
      return ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
    }
    val array = new Array[JsonNode](data.size())
    var i = 0;
    while (i < data.size() ) {
      array(i) = data.get(i)
      i = i + 1
    }
    ui = Utils.emojiFilter(ui)
    (ot, nt, dt, tec, ver, prov, ct, mn, ui, osVersion, deviceName, browser, firstChannel, ns , array)
  }


  /**
    * 数据预处理
    * @param log
    * @return
    */
  def prePerformanceMonitor(log: String): (String, String, String, String, String, String, String, String, String, String, String, String, String, Double, Array[JsonNode]) = {
    val emptyArray = new Array[JsonNode](0)
    try {
      val result = Utils.urlDecode(log, "UTF-8")
      val beginIndex = result.indexOf("data=")
      val endIndex = result.indexOf("&",beginIndex)
      if (endIndex == -1 || beginIndex == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val decodeLog = result.substring(beginIndex + 5, endIndex).replace(" ","+")

      //操作系统
      val osVersionBegin = result.indexOf("osVersion=")
      var osVersionEnd = result.indexOf("&",osVersionBegin)
      if (osVersionEnd == -1){
        osVersionEnd = result.indexOf(" ",osVersionBegin)
      }
      if (osVersionBegin == -1 || osVersionEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val osVersion = result.substring(osVersionBegin + 10, osVersionEnd)

      //设备型号
      val deviceNameBegin = result.indexOf("deviceName=")
      val deviceNameEnd = result.indexOf("&",deviceNameBegin)
      if (deviceNameBegin == -1 || deviceNameEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val deviceName = result.substring(deviceNameBegin + 11, deviceNameEnd)

      //浏览器
      val browserBegin = result.indexOf("browser=")
      val browserEnd = result.indexOf("&",browserBegin)
      if (browserBegin == -1 || browserEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val browser = result.substring(browserBegin + 8, browserEnd)

      //ip
      val ipBegin = result.indexOf("ip=")
      val ipEnd = result.indexOf("&",ipBegin)
      if (ipBegin == -1 || ipEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val ip = result.substring(ipBegin + 3, ipEnd)

      //ipToProvince
      val ipToProvinceBegin = result.indexOf("ipToProvince=")
      val ipToProvinceEnd = result.indexOf("&",ipToProvinceBegin)
      if (ipToProvinceBegin == -1 || ipToProvinceEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val ipToProvince = result.substring(ipToProvinceBegin + 13, ipToProvinceEnd)

      //ipToCity
      val ipToCityBegin = result.indexOf("ipToCity=")
      val ipToCityEnd = result.indexOf("&",ipToCityBegin)
      if (ipToCityBegin == -1 || ipToCityEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val ipToCity = result.substring(ipToCityBegin + 9, ipToCityEnd)

      //firstChannel
      val firstChannelBegin = result.indexOf("firstChannel=")
      val firstChannelEnd = result.indexOf("&",firstChannelBegin)
      if (firstChannelBegin == -1 || firstChannelEnd == -1) {
        return  ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
      }
      val firstChannel = result.substring(firstChannelBegin + 13, firstChannelEnd)

      var performanceMonitorLog = ""
      if(firstChannel.equals("WAP")){
        performanceMonitorLog = new String(Base64.decodeBase64(decodeLog), "UTF-8").replace("\\n", "")
      }else{
        performanceMonitorLog = GZipUtils.unCompress(decodeLog)
      }
      //将data参数进行json转换，得到自己想要的数据
      jsonPrasePerformanceMonitor(performanceMonitorLog,osVersion,deviceName,browser,ip,ipToProvince,ipToCity,firstChannel)
    }
    catch {
      case e: Exception =>
        logger.error(s"PerformaceMonitorLog an error occurred $log")
        ("", "", "", "", "", "", "", "", "" , "", "", "", "" ,0.0 , emptyArray)
    }
  }

  def main(args: Array[String]): Unit = {
    //    val log = "\n10.113.1.129 - - [2017-03-01T13:59:34+08:00] \"GET /monitor/1.gif?clientTimestamp=1488348533&data=H4sIAAAAAAAAA61Y204jRxD9Fz9DU1V9q1opDyTZZFFWEAWkPIQ8DPYAozUeyx6TvYgf2P%2BJkg%2BK8h2pNni9xG23hYan8VwPp885VdWfBuN28Gpwenb6enAwGHV6DBgQ4igeRmevDp2trw9FrvwhOrymq9rhFY8OF4tmpA9M5vpA1IP7epaOjBhM76m6avDqt0%2BDWXrhz2fnF3qya%2B5q/YWO2Tq2lgKQ8c4hi16tJnqtmjZm2N6ZWbvoajOvq9nw1tzU3XdVV9%2B0sw8X1dU83Xuf3mPAQPrWR/1h0ephPUzn0w3pswhI6Ue3/KeMjcSQ7hpO1//w9fJGYhIDMTLj4OHgCfWPrzdBOyTP1sQg4OwK9OXRm%2B5u/LYdVuPmY9U17eTySDGf6uF9vUK7IjiBJQobYGENNAaUkAeKznljPTLEPYCiiRGAYA30eNossX3btt3xKIdNSduFjRnYyxZsPhpLJMJFbAF07RxJkAy2al6fN119MrlucwjjDvLQBOdFwhaAVtVpCRjK5EU2kT1zDqCSdz6c1fUkB0/lvAsfRwsubsEXwDjy4KSMT3QtwKFsWKftbutZ8sxJO3/TdtfN%2B6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OO%2BqbjE//6PphreXR8NxU0%2B6C31Wz99Nv1k/nsMd4q7exK620TJtubE6fUnJ2U7bXo364CBsOLuZNJ2pRqPv6/tmWF%2B079a7hs/342Azf5ZcfumgIOanLcc6QaXduMHD7weDtltf7pavOzk71%2BNJOv/ryQ8nejyu1vdMZ23C8s/fn//986/Bw3%2BZeZMmDxgAAA%3D%3D&debug=true&deviceName=x86_64&firstChannel=IPHONE&format=json&globalLatitude=&globalLongitude=&lvidfa=3704E629-61B3-4D38-B545-2A15E2EA142B&lvsessionid=&lvtukey=8b46411ed7feebcde39042da6831fcf4&lvversion=7.9.1&netWorkType=WIFI&osVersion=10.2&secondChannel=APPSTORE&udid=016107d7-743b-43ef-99b5-141f2be41b8d-uuid HTTP/1.1\" 200 43 \"-\" \"IPHONE_APPSTORE 7.9.1 (iPhone; iOS 10.2; WIFI x86_64)\" \"-\" 0.000 -"

    //    prePerformanceMonitor(log)
    //    val log = "{\"lo\":\"NONE\",\"dt\":\"016107d7-743b-43ef-99b5-141f2be41b8d-uuid\",\"ns\":\"7\",\"ver\":\"7.9.1\",\"data\":[{\"rt\":\"POST\",\"time\":\"1488348332602.544189\",\"an\":\"api.com.route.search.getCategoryTabs\",\"av\":\"1.0.0\",\"dz\":\"313\",\"ec\":\"1\",\"at\":\"10121\",\"tt\":\"0.372803\",\"cp\":\"NONE\",\"ft\":\"12829.077881\"},{\"rt\":\"GET\",\"time\":\"1488348412583.769043\",\"an\":\"\\/HtmlLocalization\\/getNative\",\"av\":\"NONE\",\"dz\":\"226\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.761963\",\"cp\":\"NONE\",\"ft\":\"11445.351807\"},{\"rt\":\"GET\",\"time\":\"1488348412581.770020\",\"an\":\"\\/Api\\/getBootAd\",\"av\":\"NONE\",\"dz\":\"101\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.880859\",\"cp\":\"NONE\",\"ft\":\"11457.322998\"},{\"rt\":\"GET\",\"time\":\"1488348412600.042969\",\"an\":\"\\/Api\\/getBaseSiteInfo\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.645996\",\"cp\":\"NONE\",\"ft\":\"11439.132080\"},{\"rt\":\"GET\",\"time\":\"1488348412578.785889\",\"an\":\"\\/Api\\/getBootScreen\",\"av\":\"NONE\",\"dz\":\"418\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.873047\",\"cp\":\"NONE\",\"ft\":\"11460.425049\"},{\"rt\":\"GET\",\"time\":\"1488348412590.804199\",\"an\":\"api.com.other.getIosHotfix\",\"av\":\"1.0.0\",\"dz\":\"142\",\"ec\":\"1\",\"at\":\"11\",\"tt\":\"2.881104\",\"cp\":\"NONE\",\"ft\":\"11448.840820\"},{\"rt\":\"GET\",\"time\":\"1488348424262.278076\",\"an\":\"\\/Api2\\/getIos9Key\",\"av\":\"NONE\",\"dz\":\"90\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.483154\",\"cp\":\"NONE\",\"ft\":\"238.880859\"},{\"rt\":\"GET\",\"time\":\"1488348424379.579102\",\"an\":\"\\/HtmlLocalization\\/getAppImage\",\"av\":\"NONE\",\"dz\":\"100\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"9.635986\",\"cp\":\"NONE\",\"ft\":\"621.916992\"},{\"rt\":\"GET\",\"time\":\"1488348424492.475098\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.529053\",\"cp\":\"NONE\",\"ft\":\"604.501953\"},{\"rt\":\"GET\",\"time\":\"1488348424388.833984\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.684814\",\"cp\":\"NONE\",\"ft\":\"708.236084\"},{\"rt\":\"GET\",\"time\":\"1488348424402.553955\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.132080\",\"cp\":\"NONE\",\"ft\":\"695.246094\"},{\"rt\":\"GET\",\"time\":\"1488348424499.944824\",\"an\":\"\\/Api2\\/getO2OInfo\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.257812\",\"cp\":\"NONE\",\"ft\":\"623.115234\"},{\"rt\":\"GET\",\"time\":\"1488348424632.267090\",\"an\":\"\\/AutoRecommended\\/autoRecommended\",\"av\":\"NONE\",\"dz\":\"1161\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.629883\",\"cp\":\"NONE\",\"ft\":\"536.389893\"},{\"rt\":\"GET\",\"time\":\"1488348424590.760010\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"3.113037\",\"cp\":\"NONE\",\"ft\":\"580.250000\"},{\"rt\":\"GET\",\"time\":\"1488348424694.570068\",\"an\":\"\\/Api2\\/getO2OInfo\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"9.516846\",\"cp\":\"NONE\",\"ft\":\"476.574951\"},{\"rt\":\"GET\",\"time\":\"1488348424540.614014\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"10.094971\",\"cp\":\"NONE\",\"ft\":\"630.563965\"},{\"rt\":\"GET\",\"time\":\"1488348424597.437012\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"3.924072\",\"cp\":\"NONE\",\"ft\":\"573.636963\"},{\"rt\":\"GET\",\"time\":\"1488348424696.335938\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"11.455811\",\"cp\":\"NONE\",\"ft\":\"474.892090\"},{\"rt\":\"GET\",\"time\":\"1488348424687.584961\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"9.648926\",\"cp\":\"NONE\",\"ft\":\"483.579102\"},{\"rt\":\"GET\",\"time\":\"1488348424534.654053\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"12.614990\",\"cp\":\"NONE\",\"ft\":\"636.602051\"},{\"rt\":\"GET\",\"time\":\"1488348424585.057861\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"13.021240\",\"cp\":\"NONE\",\"ft\":\"586.052002\"},{\"rt\":\"GET\",\"time\":\"1488348424601.750000\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"10.886963\",\"cp\":\"NONE\",\"ft\":\"571.760986\"},{\"rt\":\"GET\",\"time\":\"1488348424699.542969\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.296875\",\"cp\":\"NONE\",\"ft\":\"491.563965\"},{\"rt\":\"GET\",\"time\":\"1488348424706.937988\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.401123\",\"cp\":\"NONE\",\"ft\":\"488.989990\"},{\"rt\":\"GET\",\"time\":\"1488348424503.541016\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"10.552246\",\"cp\":\"NONE\",\"ft\":\"667.649902\"},{\"rt\":\"GET\",\"time\":\"1488348424685.760986\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.261963\",\"cp\":\"NONE\",\"ft\":\"629.023926\"},{\"rt\":\"GET\",\"time\":\"1488348424823.739990\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"3.729980\",\"cp\":\"NONE\",\"ft\":\"498.896973\"},{\"rt\":\"GET\",\"time\":\"1488348424709.121826\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"6.124023\",\"cp\":\"NONE\",\"ft\":\"602.248047\"},{\"rt\":\"GET\",\"time\":\"1488348424690.334961\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.702148\",\"cp\":\"NONE\",\"ft\":\"632.019043\"},{\"rt\":\"GET\",\"time\":\"1488348424550.572021\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"5.422119\",\"cp\":\"NONE\",\"ft\":\"773.389893\"},{\"rt\":\"GET\",\"time\":\"1488348424823.854980\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"75\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"4.678955\",\"cp\":\"NONE\",\"ft\":\"498.811035\"},{\"rt\":\"GET\",\"time\":\"1488348424823.802002\",\"an\":\"api.com.groupbuy.getIndexSeckillList\",\"av\":\"1.0.0\",\"dz\":\"2964\",\"ec\":\"1\",\"at\":\"7\",\"tt\":\"6.870117\",\"cp\":\"NONE\",\"ft\":\"500.905029\"},{\"rt\":\"GET\",\"time\":\"1488348424702.207031\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.528564\",\"cp\":\"NONE\",\"ft\":\"620.398193\"},{\"rt\":\"GET\",\"time\":\"1488348424823.643066\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"6.938232\",\"cp\":\"NONE\",\"ft\":\"501.400879\"},{\"rt\":\"GET\",\"time\":\"1488348424704.221924\",\"an\":\"\\/Api\\/getInfos\",\"av\":\"NONE\",\"dz\":\"76\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"7.869141\",\"cp\":\"NONE\",\"ft\":\"620.685059\"},{\"rt\":\"GET\",\"time\":\"1488348425198.193115\",\"an\":\"\\/AutoRecommended\\/autoRecommended\",\"av\":\"NONE\",\"dz\":\"1161\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"1.284912\",\"cp\":\"NONE\",\"ft\":\"175.679932\"},{\"rt\":\"GET\",\"time\":\"1488348430827.291992\",\"an\":\"Api\\/statusSwitch\\/clientTimestamp=1488348430\",\"av\":\"NONE\",\"dz\":\"67\",\"ec\":\"1\",\"at\":\"0\",\"tt\":\"0.304199\",\"cp\":\"NONE\",\"ft\":\"47.316895\"},{\"rt\":\"GET\",\"time\":\"1488348444056.164062\",\"an\":\"api.com.init.addDeviceToken\",\"av\":\"1.0.0\",\"dz\":\"105\",\"ec\":\"1\",\"at\":\"12\",\"tt\":\"0.290771\",\"cp\":\"NONE\",\"ft\":\"48.434082\"}],\"ot\":\"NONE\",\"tec\":\"IOS\",\"nt\":\"WIFI\",\"la\":\"NONE\",\"prov\":\"上海\"}";
    //
    //    val gzip = GZipUtils.compress(log)
    //    print(gzip)
    val log = "172.20.13.186 - - [2018-01-18T09:54:33+08:00] \"rhino.lvmama.com\"  \"GET /monitor/1.gif?browser=Chrome%20Mobile%2059.0.3071.125&data=eyJvdCI6IkNNIiwibnQiOiI0RyIsImR0IjoiODY1NzYzMDM0MDY0ODA1IiwidGVjIjoiSDUtQU5EUk9JRCIsInZlciI6Ijgu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Tk9ORSIsInR0IjoiTk9ORSIsIm50IjoiMTg0IiwiYXQiOiJOT05FIiwiYW4iOiIvdGVtcGxhdGU0L3Byb2R1Y3QvMzM3NSIsImR6IjoiTk9ORSIsImF2IjoiTk9ORSIsInJ0IjoiR0VUIiwidGltZSI6IjE1MTYyNDA0NzA4MzciLCJlYyI6Ik5PTkUiLCJjcCI6Ik5PTkUifV19&deviceName=NONE&firstChannel=WAP&ip=49.92.186.111&ipToCity=%E5%8D%97%E4%BA%AC%E5%B8%82&ipToProvince=%E6%B1%9F%E8%8B%8F&osVersion=Android HTTP/1.1\" 200 43 \"-\" \"Apache-HttpClient/4.3.1 (java 1.5)\" \"-\" \"-\" \"-\" \"-\" (com.lvmama.rhino.analyze.client.PerformanceMonitor$)";
////    val result = Utils.urlDecode(log, "UTF-8")
//    //    println(result)
//    val beginIndex = result.indexOf("data=")
//    val endIndex = result.indexOf("&",beginIndex)
////        if (endIndex == -1 || beginIndex == -1) {
////          return  ("", "", "", "", "", "" ,0.0 , null)
////        }
//    val decodeLog = result.substring(beginIndex + 5, endIndex)
//    val result0 = Utils.urlDecode(decodeLog, "UTF-8")
//        println(result0)
//    val gzip = GZipUtils.compress(decodeLog)
//    print(gzip)
//        print(decodeLog)
val result = Utils.urlDecode(log, "UTF-8")
    prePerformanceMonitor(result)
//    val beginIndex = result.indexOf("data=")
//    val endIndex = result.indexOf("&",beginIndex)
//    val decodeLog = result.substring(beginIndex + 5, endIndex)
//    val performanceMonitorLog = GZipUtils.unCompress(decodeLog)
//    println(performanceMonitorLog)
//    println(Utils.getLastHourDate())
    println(Utils.emojiFilter("""|😁:2,嗯嗯:2,你好:2,有人工回答吗？"""))
  }
}